//
//  MainController.swift
//  01weibo
//
//  Created by 李想 on 16/6/2.
//  Copyright © 2016年 李想. All rights reserved.
///Users/lixiang/Desktop/平时代码练习/作业/Swift/weibo/01weibo/01weibo/01weibo.xcodeproj Missing "Default-568h@2x.png" launch image
//tabBar的子view是在viewWillAppear才创建，定制tabBar时，需要将定制的子view移到最上层： bringSubviewToFront

import UIKit
//MARK: - class里面只放生命周期,构造方法,属性
class MainController: UITabBarController {
    let uiview: UIView = UIView()
    //构造一个button
  private lazy var composeButton: UIButton = UIButton(title: "",  imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button", target: nil, action: "compose")
    

    override func viewDidLoad() {
        
        super.viewDidLoad()
        //添加所有子控制器
        addChildViewControllers()
        //添加构造按钮
        addComposeButton(composeButton)
        //设置tabBar
        settabBar()
        
        
 
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        tabBar.bringSubviewToFront(composeButton)
    }


}

//MARK: - 分类可以将相同逻辑的代码放到一个分类里,代码比较易读,层次相对清晰
extension MainController {
    //添加所有子控制器
  private  func addChildViewControllers() {
        //添加首页
        let homeController = HomeController()
        //添加子控制器样式
        addChildViewController(homeController, color: UIColor.redColor(), title: "首页", imageName: "tabbar_home")
        
        //添加消息页
        let  messageController = MessageController ()
        addChildViewController(messageController, color: UIColor.greenColor(), title: "消息", imageName: "tabbar_message_center")
        //添加占位控制器
        let viewController = UIViewController()
        addChildViewController(viewController, color: UIColor.grayColor(), title: nil, imageName: nil)
        
        //添加发现页
        let  discoverController = DiscoverController()
        addChildViewController(discoverController, color: UIColor.blueColor(), title: "发现", imageName: "tabbar_discover")
        
        //添加我页
        let  profileController = ProfileController ()
        addChildViewController(profileController, color: UIColor.yellowColor(), title: "我", imageName: "tabbar_profile")
    }
    //添加子控制器样式
  private  func addChildViewController(childController: UIViewController,   color: UIColor,  title: String?,  imageName:String? ) {
        
        let navigationHome = UINavigationController(rootViewController: childController)
        addChildViewController(navigationHome)
        
        childController.view.backgroundColor = color
        childController.title = title
    //定制tabbar文字和颜色
    childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Normal)
    childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
    
        if imageName != nil {
            
        childController.tabBarItem.image = UIImage(named: imageName!)?.imageWithRenderingMode(.AlwaysOriginal)
            
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        }
    }
    
    //添加构造按钮的方法
  private  func addComposeButton(composeButton: UIButton){
        //确定按钮的宽(tabBar是一个视图)
        let buttonWidth = tabBar.bounds.width/5
        //确定按钮的frame
        let buttonFrame = CGRectInset(tabBar.bounds, 2 * buttonWidth, 0)
        //frame赋值
        composeButton.frame = buttonFrame
        //添加构造好的按钮到tabBar视图上
        tabBar.addSubview(composeButton)

    }
    
    //构造按钮的点击事件
  private  func compose (){
        print("恭喜发财")
    }
    
    private func settabBar(){
//        //设置tabbar的渲染染色(方法一)
//        tabBar.tintColor = UIColor.orangeColor()
        //设置tabbar的阴影和背影颜色
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.shadowImage = UIImage.creatAddImage(UIColor(white: 0.8, alpha: 0.8))
    }
    
}
